from service.data import BusinessErrorCode, BusinessErrorMes
from service.handle_format import get_public_private_attribute, get_handle_to_proxy, get_company_info, request_handle_proxy
from service.common import insert_data, find_data, update_data, delete_data, get_condition
from pao_python.pao.data import process_db, dataframe_to_list, data_to_string_date, JsonRpc2Error, DataProcess
import json
import requests
import pymongo
import pandas as pd
from pao_python.pao.service.security.security_service import LoginService
import uuid
import time


class StencilManageMent(DataProcess):
    # 查询模版列表
    def get_stencil_list(self, condition, page, count):
        res = {'result': []}

        def process_func(db):
            nonlocal res
            cols = db['IDMIS_Stencil']
            new_condition = get_condition(condition)
            if page and count:
                data_list = list(cols.find(new_condition)[
                                 (page-1)*count:(page-1)*count+count])
            else:
                data_list = list(cols.find(new_condition)[:])
            res['total'] = cols.find(new_condition).count()
            if len(data_list) > 0:
                for x in data_list:
                    otherStyleTime = data_to_string_date(x['create_date'])
                    x['create_date'] = otherStyleTime
                pd_equit = pd.DataFrame(data_list).drop(['_id'], axis=1)
                res['result'] = dataframe_to_list(pd_equit)

        process_db(self.db_addr, self.db_port, 'IDMIS', process_func)
        return res

    # 获取模版信息
    def get_stencil(self, stencil_id):
        res = {}

        def process_func(db):
            nonlocal res
            stencil_mes = find_data(db, 'IDMIS_Stencil', {'id': stencil_id})
            if len(stencil_mes) > 0:
                res = dataframe_to_list(pd.DataFrame(
                    stencil_mes).drop(['_id'], axis=1))[0]
        process_db(self.db_addr, self.db_port, 'IDMIS', process_func)
        return res

    # 删除模版信息
    def del_stencil(self, stencil_ids):
        res = False

        def process_func(db):
            nonlocal res
            del_handle = delete_data(db, 'IDMIS_Stencil', {
                                     'id': {'$in': stencil_ids}})
            if del_handle:
                res = True
        process_db(self.db_addr, self.db_port, 'IDMIS', process_func)
        return res

    # 新增/修改模版信息
    def insert_stencil(self, data, is_insert):
        res = False

        def process_func(db):
            data_info = get_company_info(data)
            if is_insert:
                stencil_result = insert_data(db, 'IDMIS_Stencil', data_info)
                if stencil_result:
                    res = True
            else:
                stencil_result = update_data(
                    db, 'IDMIS_Stencil', data_info, {'id': data['id']})
                if stencil_result:
                    res = True
        process_db(self.db_addr, self.db_port, 'IDMIS', process_func)
        return res
